using System;
using System.Data;
using System.Diagnostics;
using MyClever.Lib;

namespace MyClever.DefaultPlugins
{
    public class StartExe : MissionPlugin
    {
        [Package(Direction.Input, "Defines the execution file, you like to start.")]
        enum Executable
        {
            [Column(typeof(string), "The file you like to process? (*.exe)",
                Validation="isexistingfile")]
            FilePath
        }

        [Package(Direction.Input, "Defines the arguments of the process..")]
        enum CommandLine
        {
            [Column(typeof(string), "The arguments you like to pass.")]
            URL
        }

        [Plugin(PluginDevStatus.Production, "Starts the given process-file (*.exe) with your expected command line.")]
        public StartExe()
        {
        }

        public override void Initialize()
        {
            
            //This is the first Function called by the host...
            //Put anything needed to start with here first
        }

        public override void Dispose()
        {
            //Put any cleanup code in here for when the program is stopped
        }

        /// <summary>
        /// Generate new Source-content.
        /// </summary>
        protected override void Work()
        {
            DataTableReader executableFiles = this.GetDataReader(typeof(Executable));
            while (executableFiles.Read())
            {
                this.AddNewLoggingMessage(typeof(Executable), String.Format("execute:{0}", executableFiles[Executable.FilePath.ToString()].ToString()));

                DataTableReader commandLines = this.GetDataReader(typeof(CommandLine));
                while (commandLines.Read())
                {
                    this.AddNewLoggingMessage(typeof(CommandLine), String.Format("with argument:{0}", commandLines[CommandLine.URL.ToString()].ToString()));

                    Process.Start(executableFiles[Executable.FilePath.ToString()].ToString(), commandLines[CommandLine.URL.ToString()].ToString()).WaitForExit();

                }
            }
        }
    }
}
